package org.nobody.mallb2c.mallproduct.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.nobody.mallb2c.mallproduct.entity.ProductAttrValueEntity;
import org.nobody.mallb2c.mallproduct.service.ProductAttrValueService;
import org.nobody.mallb2c.mallproduct.vo.AttrResVo;
import org.nobody.mallb2c.mallproduct.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.nobody.mallb2c.mallproduct.service.AttrService;
import org.nobody.common.utils.PageUtils;
import org.nobody.common.utils.R;


/**
 * 商品属性
 *
 * @author fancier
 * @email fancier2093@gmail.com
 * @date 2020-07-30 16:25:27
 */
@RestController
@RequestMapping("mallproduct/attr")
public class AttrController {
  @Autowired
  private AttrService attrService;

  @Autowired
  ProductAttrValueService productAttrValueService;

  @GetMapping("/base/listforspu/{spuId}")
  public R baseAttrlistforspu(@PathVariable("spuId")Long spuId){
    List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);
    return R.ok().put("data", entities);
  }

  @GetMapping("/{attrType}/list/{catelogId}")
  public R baseAttrList(
    @RequestParam Map<String, Object> params,
    @PathVariable("catelogId") Long catelogId,
    @PathVariable("attrType") String type){

    PageUtils page = attrService.queryBaseOrSaleAttrPage(params, catelogId, type);
    return R.ok().put("page", page);
  }

  /**
   * 列表
   */
  @RequestMapping("/list")
  //@RequiresPermissions("mallproduct:attr:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = attrService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{attrId}")
  //@RequiresPermissions("mallproduct:attr:info")
  public R info(@PathVariable("attrId") Long attrId) {
    //AttrEntity attr = attrService.getById(attrId);
    AttrResVo resVo = attrService.getAttrInfo(attrId);

    return R.ok().put("attr", resVo);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("mallproduct:attr:save")
  public R save(@RequestBody AttrVo attr) {
    attrService.saveAttr(attr);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("mallproduct:attr:update")
  public R update(@RequestBody AttrVo attr) {
    attrService.updateAttr(attr);

    return R.ok();
  }

  @PostMapping("/update/{spuId}")
  public R updateSpuAttr(
    @PathVariable("spuId") Long spuId,
    @RequestBody List<ProductAttrValueEntity> entities
  ) {
    productAttrValueService.updateSpuAttr(spuId, entities);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("mallproduct:attr:delete")
  public R delete(@RequestBody Long[] attrIds) {
    attrService.removeByIds(Arrays.asList(attrIds));

    return R.ok();
  }

}
